<template>
  <FrontSideTable
    dataUrl="/param/positionType/getList"
    addUrl="/param/positionType/add"
    editUrl="/param/positionType/edit"
    deleteUrl="/param/positionType/delete"
    tableTitle="部位列表"
    searchHint="根据部位名称查找"
    :columns="columns"
    :form="form"
    :rules="rules"
    :searchProps="searchProps"
    @beforeAdd="beforeAdd"
    @beforeEdit="beforeEdit"
    @onCreated="onCreated">
    <template slot="form">
      <el-form-item label="部位名称" prop="name">
        <el-input v-model="form.name" placeholder="输入部位名称"></el-input>
      </el-form-item>
      <el-form-item label="部位权重" prop="weight">
        <el-input type="number" v-model.number="form.weight" placeholder="输入部位权重"></el-input>
      </el-form-item>
      <el-form-item label="顺序号" prop="sortNo">
        <el-input type="number" v-model.number="form.sortNo" placeholder="输入菜单排序号"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.notes"></el-input>
      </el-form-item>
    </template>
  </FrontSideTable>
</template>

<script>
  import FrontSideTable from "../../template/FrontSideTable.vue"
  import {clearFields} from "../../../assets/js/utils/ObjectUtils"
  import {intValidator, floatValidator} from "../../../assets/js/utils/ValidateUtils"

  let oldName = "";

  export default {
    name:"PositionTypeMaintenance",
    data() {
      //部位名称校验
      let nameValidator = (rule, value, callback)=>{
        if(value === oldName) {
            callback();
        } else if(this.dataList.some(item=>item.name === value)) {
            callback(new Error("部位名称重复"));
        } else {
            callback();
        }
      };

      return {
        dataList:[],
        form:{
          name:"",
          weight:0,
          notes:"",
          sortNo:0
        },
        searchProps:["name"],
        columns:[
          {prop:"sortNo", label:"顺序号", sortable:"custom", width:"100"},
          {prop:"name", label:"部位名称"},
          {prop:"weight", label:"部位权重"}
        ],
        rules:{
          name:[
            {required:true, message:"请输入部位名称", trigger:"blur"},
            {validator:nameValidator, trigger:"blur"}
          ],
          weight:[
            {type:"number", required:true, message:"请输入权重", trigger:"blur"},
            {validator:floatValidator, trigger:"blur"}
          ],
          sortNo:[
            {validator:intValidator, trigger:"blur"}
          ]
        },
      }
    },
    methods:{
      onCreated(dataList) {
        this.dataList = dataList;
      },
      beforeAdd() {
        oldName = "";
        clearFields(this.form);
      },
      beforeEdit(data) {
        oldName = data.name;
        this.form = {...data};
      }
    },
    components:{FrontSideTable}
  }
</script>



// WEBPACK FOOTER //
// PositionTypeMaintenance.vue?91a2c9c2